1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| #include<bits/stdc++.h> using namespace std; typedef unsigned int u32;
const int ONE = 1000005; const int MOD = 1e9 + 7; const u32 INF = 4294967295u;
int n, Q; int k; struct point { u32 d, v; }a[ONE], L, R; bool cmp(const point &a, const point &b) {return a.d < b.d;}
struct power { u32 val[7]; friend power operator +(power a, power b) { power c; for(int i = 1; i <= 6; i++) c.val[i] = a.val[i] + b.val[i]; for(int i = 1; i <= 6; i++) for(int j = 1; j < i; j++) c.val[i] += a.val[j] * b.val[i - j]; return c; } }Node[ONE], A[3], Ans;
struct Min { u32 val, pos; friend Min operator +(Min a, Min b) { Min c = (Min){INF, 0}; if(a.val < c.val) c = a; if(b.val < c.val) c = b; return c; } }Val[ONE], res_min;
int get() { int res=1,Q=1; char c; while( (c=getchar())<48 || c>57) if(c=='-')Q=-1; if(Q) res=c-48; while((c=getchar())>=48 && c<=57) res=res*10+c-48; return res*Q; }
namespace Seg { void Build(int i, int l, int r) { Val[i] = (Min){INF, 0}; for(int j = 1; j <= 6; j++) Node[i].val[j] = 0; if(l == r) { Node[i].val[1] = a[l].v; Val[i] = (Min){a[l].v, l}; return; } int mid = l + r >> 1; Build(i << 1, l, mid); Build(i << 1 | 1, mid + 1, r); Node[i] = Node[i << 1] + Node[i << 1 | 1]; Val[i] = Val[i << 1] + Val[i << 1 | 1]; }
void Find(int i, int l, int r, int L, int R) { if(L > R) return; if(L <= l && r <= R) { res_min = res_min + Val[i]; return; } int mid = l + r >> 1; if(L <= mid) Find(i << 1, l, mid, L, R); if(mid + 1 <= R) Find(i << 1 | 1, mid + 1, r, L, R); }
void Query(int i, int l, int r, int L, int R, int opt) { if(L > R) return; if(L <= l && r <= R) { A[opt] = A[opt] + Node[i]; return; } int mid = l + r >> 1; if(L <= mid) Query(i << 1, l, mid, L, R, opt); if(mid + 1 <= R) Query(i << 1 | 1, mid + 1, r, L, R, opt); } }
void Deal(int k) { int Left = lower_bound(a + 1, a + n + 1, L, cmp) - a; int Right = upper_bound(a + 1, a + n + 1, R, cmp) - a - 1; if(Left >= Right) {printf("0\n"); return;}
res_min = (Min){INF, 0}; Seg::Find(1, 1, n, Left, Right);
for(int i = 1; i <= 6; i++) A[1].val[i] = A[2].val[i] = 0; Seg::Query(1, 1, n, Left, res_min.pos - 1, 1); Seg::Query(1, 1, n, res_min.pos + 1, Right, 2);
Ans = A[1] + A[2]; for(u32 i = 1; i <= k; i++) Ans.val[k] *= i; printf("%u\n", Ans.val[k]); }
int main() { n = get(); Q = get(); for(int i = 1; i <= n; i++) a[i].d = get(); for(int i = 1; i <= n; i++) a[i].v = get(); sort(a + 1, a + n + 1, cmp);
Seg::Build(1, 1, n);
while(Q--) { L.d = get(), R.d = get(), k = get(); Deal(k); } }
|